import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


public class UserInterface {

	Display display = new Display();
	Shell shell = new Shell(display, SWT.SHELL_TRIM);

	private String currentPath;

	private ProtocolTable protocolTable;
	private Text styledText;

	private void run() {
		shell.setText("Compatibilator");
		shell.setMinimumSize(300, 300);

		GridLayout gridLayout = new GridLayout(3, false);
		gridLayout.verticalSpacing = 8;

		shell.setLayout(gridLayout);

		GridData gridData;

		Label directory = new Label(shell, SWT.NULL);
		directory.setText("Directory: ");

		styledText = new Text(shell, SWT.SINGLE | SWT.BORDER);
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		gridData.grabExcessHorizontalSpace = true;
		styledText.setLayoutData(gridData);
		styledText.addKeyListener(new KeyListener() {
			
			@Override
			public void keyReleased(KeyEvent e) {
				currentPath = styledText.getText();
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
			}
		});

		Button browse = new Button(shell, SWT.PUSH);
		browse.setText("Browse");
		browse.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String path = askForDirectory();
				if (!(path == null)) {
					currentPath = path;
					styledText.setText(currentPath);
				}
			}
		});

		Button startButton = new Button(shell, SWT.PUSH);
		startButton.setText("Make Compatible!!!");
		startButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(currentPath == null){
					showError("Provide a path first!");
					return;
				}
				Compatibilator compatibilator = new Compatibilator(protocolTable);
				protocolTable.clear();
				boolean wasSuccessful = compatibilator.run(currentPath);
				if (!wasSuccessful) {
					showError(compatibilator.getErrorMessage());
				}
			}
		});
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		gridData.horizontalSpan = 3;
		startButton.setLayoutData(gridData);

		protocolTable = new ProtocolTable(shell, display);

		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

	/**
	 * Opens a prompt and let the user browse through his file system and choose
	 * a directory.
	 * 
	 * @return the directory path
	 */
	private String askForDirectory() {
		DirectoryDialog dialog = new DirectoryDialog(shell);
		String platform = SWT.getPlatform();
		dialog.setFilterPath(platform.equals("win32") || platform.equals("wpf") ? "c:\\"
				: "/");
		return dialog.open();
	}


	/**
	 * 
	 * @param errorMessage
	 */
	private void showError(String errorMessage) {
		Status status = new Status(IStatus.ERROR, "ID", 0,
				errorMessage, null);
		 ErrorDialog.openError(Display.getCurrent().getActiveShell(),
		            null, errorMessage, status);
	}

	public static void main(String... args) {
		new UserInterface().run();
	}

}
